﻿package ${Package}.controller;

import com.alibaba.fastjson.JSONObject;
import ${Package}.bo.BaseBo;
import ${Package}.bo.impl.${table.TableNameAlias}Bo;
import ${Package}.model.${table.TableNameAlias};
import ${Package}.model.SysMenu;
import ${Package}.security.Authority;
import ${Package}.security.PermissionType;
import ${Package}.utils.Base64;
import ${Package}.utils.ReturnResult;
import ${Package}.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:${Utils.GetDate} ${Utils.GetTime}
*/ 
@Controller
@RequestMapping(value = "${table.TableNameAlias}")
public class ${table.TableNameAlias}Controller{
    Logger log = Logger.getLogger(${table.TableNameAlias}Controller.class);
    private static final String MODULECODE = "${table.TableNameAlias}";
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.View)
    @RequestMapping(value = "view", method = RequestMethod.GET)
    public ModelAndView view(HttpServletRequest request) {
        Integer id = StringUtils.stringToInteger(request.getParameter("id"));
        ${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
        ModelAndView mv = new ModelAndView();
        if (id != null) {
            ReturnResult result = bo.selectById(id);
            log.debug(JSONObject.toJSONString(result));
            mv.addObject("info", result);
        }
        return mv;
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Delete)
    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    public String delete(HttpServletRequest request) {
        String ids = request.getParameter("id");
        ${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
        ReturnResult result = bo.delByIds(ids);
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.On)
    @RequestMapping(value = "audit", method = RequestMethod.POST)
    @ResponseBody
    public String audit(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        Integer state = StringUtils.stringToInteger(request.getParameter("state"));
        ReturnResult result = new ReturnResult();
        if (!StringUtils.isEmpty(ids)) {
            String[] idArr = ids.split(",");
            ${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
            result = bo.audit(state, idArr);
        } else {
            result.setCode(100005000);
        }
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Edit)
    @RequestMapping(value = "do/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(@ModelAttribute ${table.TableNameAlias} entity) {
        BaseBo bo = new ${table.TableNameAlias}Bo();
        ReturnResult result = bo.editByModel(entity);
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Add)
    @RequestMapping(value = "do/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@ModelAttribute ${table.TableNameAlias} entity) {
        BaseBo bo = new ${table.TableNameAlias}Bo();
        ReturnResult result = bo.insertByModel(entity);
        return JSONObject.toJSONString(result);
    }
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.List)
    @RequestMapping("getList")
    @ResponseBody
    public String getList(HttpServletRequest request,@RequestParam(value = "page", required = true, defaultValue = "1") Integer pageIndex,
                          @RequestParam(value = "rows", required = true, defaultValue = "10") Integer pageSize) {
        ${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
        Map<String, Object> map =StringUtils.getSearchMap(request);
        ReturnResult result = bo.selectByMap(pageIndex,pageSize,map);
        return JSONObject.toJSONString(result);
    }

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.Sort)
    @RequestMapping(value = "sort/{sorts}/{ids}")
    @ResponseBody
    public String sort(@PathVariable("sorts") String sorts, @PathVariable("ids") String ids) {
        ${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
    	sorts=Base64.encodereplace(sorts);
		ids=Base64.encodereplace(ids);
         sorts = new String(Base64.decode(sorts.toCharArray()));
         ids = new String(Base64.decode(ids.toCharArray()));
        ReturnResult result = bo.sort(sorts, ids);

        return JSONObject.toJSONString(result);
    }

}